package org.example.script;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.leetcode.core.dao.TQuestionMapper;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtils {
    public static <T extends BaseMapper> T getMybatisMapper(Class c) throws IOException {
        String resource = "Mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return (T) sqlSession.getMapper(c);
    }
}
